package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.jdfsystem.cache.PlatformInfoCache;
import cn.juque.jdfsystem.entity.PlatformInfo;
import cn.juque.jdfsystem.service.IPlatformInfoService;
import cn.juque.lucenecandy.core.base.DeleteByIdsWrapperBuilder;
import cn.juque.lucenecandy.core.base.IdQueryWrapperBuilder;
import cn.juque.lucenecandy.core.base.QueryWrapperBuilder;
import cn.juque.lucenecandy.core.enums.MatchTypeEnum;
import cn.juque.lucenecandy.helper.IndexHelper;
import org.apache.lucene.search.BooleanClause;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>接入的平台信息</li>
 * </ul>
 * @date 2023-01-13 15:18:08
 **/
@Service("platformInfoService")
public class PlatformInfoServiceImpl implements IPlatformInfoService {

    @Resource
    private IndexHelper indexHelper;

    @Resource
    private PlatformInfoCache platformInfoCache;

    /**
     * 分页查询
     *
     * @param pageInfo     pageInfo
     * @param platformInfo platformInfo
     * @return DataGrid
     */
    @Override
    public List<PlatformInfo> listInfoWithPage(PageInfo pageInfo, PlatformInfo platformInfo) {
        QueryWrapperBuilder<PlatformInfo> queryWrapperBuilder = this.buildQuery(platformInfo);
        queryWrapperBuilder.pageInfo(pageInfo);
        return this.indexHelper.searchByPage(queryWrapperBuilder.build());
    }

    /**
     * 查询
     *
     * @param platformInfo platformInfo
     * @return List
     */
    @Override
    public List<PlatformInfo> listInfoWithoutPage(PlatformInfo platformInfo) {
        QueryWrapperBuilder<PlatformInfo> queryWrapperBuilder = this.buildQuery(platformInfo);
        return this.indexHelper.search(queryWrapperBuilder.build());
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return PlatformInfo
     */
    @Override
    public PlatformInfo detail(String id) {
        IdQueryWrapperBuilder<PlatformInfo> idQueryWrapperBuilder = new IdQueryWrapperBuilder<>(PlatformInfo.class, id);
        return this.indexHelper.searchById(idQueryWrapperBuilder.build());
    }

    /**
     * 保存
     *
     * @param platformInfo platformInfo
     */
    @Override
    public void saveInfo(PlatformInfo platformInfo) {
        platformInfo.setSecret(IdUtil.simpleUUID());
        this.indexHelper.addDocument(platformInfo);
        this.platformInfoCache.refresh();
    }

    /**
     * 更新
     *
     * @param platformInfo platformInfo
     */
    @Override
    public void updateInfo(PlatformInfo platformInfo) {
        this.indexHelper.updateDocumentById(platformInfo);
        this.platformInfoCache.refresh();
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        DeleteByIdsWrapperBuilder<PlatformInfo> deleteByIdsWrapperBuilder = new DeleteByIdsWrapperBuilder<>(PlatformInfo.class, ids);
        this.indexHelper.deleteByIds(deleteByIdsWrapperBuilder.build());
        this.platformInfoCache.refresh();
    }

    private QueryWrapperBuilder<PlatformInfo> buildQuery(PlatformInfo platformInfo) {
        QueryWrapperBuilder<PlatformInfo> queryWrapperBuilder = new QueryWrapperBuilder<>(PlatformInfo.class);
        if (CharSequenceUtil.isNotEmpty(platformInfo.getPlatformId())) {
            queryWrapperBuilder.matchStr(PlatformInfo::getPlatformId, platformInfo.getPlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        }
        if (CharSequenceUtil.isNotEmpty(platformInfo.getPlatformName())) {
            queryWrapperBuilder.matchStr(PlatformInfo::getPlatformName, platformInfo.getPlatformName(), MatchTypeEnum.ALL_LIKE, BooleanClause.Occur.MUST);
        }
        return queryWrapperBuilder;
    }
}
